package com.icore.bean;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.json.JSONException;
import org.json.JSONObject;

import com.icore.AppException;

/**
 * 购车基本信息
 * 
 */
public class GoucheBaseInfo extends BaseEntity {
	private String carNo;
	private String createData;
	private String customerName;
	private String customerHomeAddr;
	private String customerCompanyAddr;
	private String customerPhone;

	public String getCarNo() {
		return carNo;
	}

	public void setCarNo(String carNo) {
		this.carNo = carNo;
	}

	public String getCreateData() {
		return createData;
	}

	public void setCreateData(String createData) {
		this.createData = createData;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getCustomerHomeAddr() {
		return customerHomeAddr;
	}

	public void setCustomerHomeAddr(String customerHomeAddr) {
		this.customerHomeAddr = customerHomeAddr;
	}

	public String getCustomerCompanyAddr() {
		return customerCompanyAddr;
	}

	public void setCustomerCompanyAddr(String customerCompanyAddr) {
		this.customerCompanyAddr = customerCompanyAddr;
	}

	public String getCustomerPhone() {
		return customerPhone;
	}

	public void setCustomerPhone(String customerPhone) {
		this.customerPhone = customerPhone;
	}

	public static GoucheBaseInfo parse(InputStream stream) throws AppException {
		GoucheBaseInfo baseinfo = new GoucheBaseInfo();

		try {
			String res = readByByte(stream, -1, new ByteArrayOutputStream(1024));
			if (res != null && res.length() > 0) {
				try {
					JSONObject resObj = new JSONObject(res);
					int code = resObj.getInt("code");
					baseinfo.setResCode(code);
					// 失败:{code:0,description:数据库语句异常,认证失败}
					if (code == 0) {
						baseinfo.setResMessage(resObj.getString("description"));
					}
					// 成功:{"data":{"create_date":"2013/12/26","customerhome_add":"香港","customer_phone":"13899998888","customer_name":"刘德华","customercompany_add":"全球","car_no":"20131226A005"},"descript":"认证成功","code":1}
					else if (code == 1) {
						baseinfo.setResMessage(resObj.getString("description"));

						String dataStr = resObj.getString("data");
						JSONObject dataObj = new JSONObject(dataStr);
						baseinfo.setCreateData(dataObj.getString("create_date"));
						baseinfo.setCustomerHomeAddr(dataObj.getString("customerhome_add"));
						baseinfo.setCustomerPhone(dataObj.getString("customer_phone"));
						baseinfo.setCustomerName(dataObj.getString("customer_name"));
						baseinfo.setCustomerCompanyAddr(dataObj.getString("customercompany_add"));
						baseinfo.setCarNo(dataObj.getString("car_no"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		} catch (AppException e) {
			e.printStackTrace();
			throw AppException.io(e);
		}
		return baseinfo;
	}

}
